<template>
  <div class="jcx-icon-select">
    <div :class="{'hasValue' : value}" class="jcx-icon-select__wrapper" @click="open">
      <el-input style="cursor: pointer;" v-model="value" :disabled="disabled" :prefix-icon="value || 'el-icon-plus'"
                readonly></el-input>
    </div>
    <el-dialog v-model="dialogVisible" :width="900" append-to-body destroy-on-close :close-on-click-modal="false"
               title="图标选择器">
      <div class="jcx-icon-select__dialog">
        <el-form>
          <el-form-item prop="searchText">
            <el-input v-model="searchText" class="jcx-icon-select__search-input" clearable placeholder="搜索"
                      prefix-icon="el-icon-search" size="large"></el-input>
          </el-form-item>
        </el-form>
        <el-tabs>
          <el-tab-pane v-for="item in data" :key="item.name" lazy>
            <template #label>
              {{ item.name }}
              <el-tag size="small" type="info">{{ item.icons.length }}</el-tag>
            </template>
            <div class="jcx-icon-select__list">
              <el-scrollbar>
                <ul @click="selectIcon">
                  <el-empty v-if="item.icons.length===0" :image-size="100" description="未查询到相关图标"></el-empty>
                  <li v-for="icon in item.icons" :key="icon">
                    <span :data-icon="icon"></span>
                    <el-icon>
                      <component :is="icon"></component>
                    </el-icon>
                  </li>
                </ul>
              </el-scrollbar>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <template #footer>
        <el-button text @click="clear">清除</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import config from "./iconSelect";
import {onMounted, ref, watch} from "vue";

const emit = defineEmits(['update:modelValue']);
const props = defineProps({
  modelValue: {
    type: String,
    default: ""
  },
  disabled: {
    type: Boolean,
    default: false
  },
})
const value = ref("");
const dialogVisible = ref(false);
const data = ref([]);
const searchText = ref("");
const open = () => {
  if (props.disabled) {
    return false
  }
  dialogVisible.value = true
}
const selectIcon = (e) => {
  if (e.target.tagName !== 'SPAN') {
    return false
  }
  value.value = e.target.dataset.icon;
  dialogVisible.value = false
}
const clear = () => {
  value.value = "";
  dialogVisible.value = false;
}
const search = (text) => {
  if (text) {
    const filterData = JSON.parse(JSON.stringify(config.icons))
    filterData.forEach(t => {
      t.icons = t.icons.filter(n => n.includes(text))
    })
    data.value = filterData
  } else {
    data.value = JSON.parse(JSON.stringify(config.icons))
  }
}

onMounted(() => {
  value.value = props.modelValue;
  data.value.push(...config.icons);
});

watch(() => props.modelValue, (val) => {
  value.value = val;
});

watch(() => value.value, (val) => {
  emit('update:modelValue', val);
});

watch(() => searchText.value, (val) => {
  search(val);
});
</script>

<style scoped>
.jcx-icon-select {
  display: inline-flex;
}

.jcx-icon-select__wrapper {
  cursor: pointer;
  display: inline-flex;
}

.jcx-icon-select__wrapper:deep(.el-input__wrapper) {
  cursor: pointer;
}

.jcx-icon-select__wrapper:deep(.el-input__wrapper).is-focus {
  box-shadow: 0 0 0 1px var(--el-input-hover-border-color) inset;
}

.jcx-icon-select__wrapper:deep(.el-input__inner) {
  flex-grow: 0;
  width: 0;
}

.jcx-icon-select__wrapper:deep(.el-input__icon) {
  margin: 0;
  font-size: 16px;
}

.jcx-icon-select__wrapper.hasValue:deep(.el-input__icon) {
  color: var(--el-text-color-regular);
}

.jcx-icon-select__list {
  height: 270px;
  overflow: auto;
}

.jcx-icon-select__list ul {
}

.jcx-icon-select__list li {
  display: inline-block;
  width: 80px;
  height: 80px;
  margin: 5px;
  vertical-align: top;
  transition: all 0.1s;
  border-radius: 4px;
  position: relative;
}

.jcx-icon-select__list li span {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  cursor: pointer;
}

.jcx-icon-select__list li i {
  display: inline-block;
  width: 100%;
  height: 100%;
  font-size: 26px;
  color: #6d7882;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 4px;
}

.jcx-icon-select__list li:hover {
  box-shadow: 0 0 1px 4px var(--el-color-primary);
  background: var(--el-color-primary-light-9);
}

.jcx-icon-select__list li:hover i {
  color: var(--el-color-primary);
}
</style>
